package com.qiantu.phone.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.http.listener.HttpCallback;
import com.lxj.xpopup.XPopup;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.db.LLFloorDBManager;
import com.qiantu.api.db.LLSceneDBManager;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.FloorBean;
import com.qiantu.api.entity.MemberBean;
import com.qiantu.api.entity.RoomBean;
import com.qiantu.api.entity.SceneBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.aop.Permissions;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.ui.adapter.RoomFloorDialogAdapter;
import com.qiantu.phone.ui.dialog.AreaSelectDialog;
import com.qiantu.phone.ui.dialog.BottomAddMemberDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class MemberRoomActivity extends AppActivity {
    private MemberBean memberBean;
    private AppCompatTextView commonArea;
    private boolean commonAreaIsSelect = true;
    private RecyclerView floorRoomList;
    private TextView save;
    private RoomFloorDialogAdapter mAdapter;
    private Map<String, RoomBean> mSelectArea = new LinkedHashMap<>();
    private List<FloorBean> mFloorBeans;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_member_room;
    }

    @Override
    protected void initView() {
        commonArea = (AppCompatTextView) findViewById(R.id.common_area);
        floorRoomList = (RecyclerView) findViewById(R.id.floor_room_list);
        save = (TextView) findViewById(R.id.save);
        setOnClickListener(R.id.common_area,R.id.save,R.id.reset);
        save.setText(R.string.ok);
        commonArea.setText(R.string.common);
        save.setEnabled(true);

        floorRoomList.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new RoomFloorDialogAdapter(getContext()) {
            @Override
            public void onSelectChanged(Map<String, RoomBean> selectBean) {

            }

        };
        floorRoomList.setAdapter(mAdapter);
    }


    @Override
    protected void initData() {
        mFloorBeans = LLFloorDBManager.getInstance(getContext()).getNotEmptyRoomFloors();
        mFloorBeans.remove(0);
        mAdapter.setData(mFloorBeans);
        memberBean = (MemberBean) getIntent().getSerializableExtra("data");
        if(memberBean == null || memberBean.getPermissions() == null){
            resetData();
        }else{
            mSelectArea = new HashMap<>();
            commonAreaIsSelect = false;
            for(MemberBean.MemberPermission permission : memberBean.getPermissions()){
                if(permission.getTargetContent().equals(RoomBean.ROOM_COMMON_SERIALNO)){
                    commonAreaIsSelect = true;
                    commonArea.setSelected(true);
                    continue;
                }
                for(FloorBean floor : mFloorBeans){
                    Boolean isHave = false;
                    for(RoomBean roomBean : floor.getRooms()){
                        if(roomBean.getRoomSerialNo().equals(permission.getTargetContent())){
                            mSelectArea.put(roomBean.getRoomSerialNo(),roomBean);
                            isHave = true;
                            break;
                        }
                    }
                    if(isHave){
                        break;
                    }
                }
            }
            mAdapter.setSelectMap(mSelectArea);
        }
    }

    private void resetData(){
        mSelectArea = new HashMap<>();
        commonAreaIsSelect = true;
        for(FloorBean floor : mFloorBeans){
            List<RoomBean> roomBeans = floor.getRooms();
            for(RoomBean roomBean : roomBeans){
                mSelectArea.put(roomBean.getRoomSerialNo(),roomBean);
            }
        }
        mAdapter.setSelectMap(mSelectArea);
        commonArea.setSelected(true);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void createMember(List<MemberBean.MemberPermission> memberPermissions) {
        LLHttpManager.inviteMember(this, AppApplication.getInstance().getCurrentHouseSerialNo(), getString("phone"), getString("remark"),"2", memberPermissions, new HttpCallback<HttpData<MemberBean>>(this){
            @Override
            public void onSucceed(HttpData<MemberBean> result) {
                super.onSucceed(result);
                if (result.getData()!=null){
                    finish();
                }
            }
        });
    }

    private void changePermission(List<MemberBean.MemberPermission> memberPermissions){
        LLHttpManager.changeRolePermission(this, AppApplication.getInstance().getCurrentHouseSerialNo(), memberBean.getUserSerialNo(), memberPermissions, new HttpCallback<HttpData<Void>>(this){
            @Override
            public void onSucceed(HttpData<Void> result) {
                super.onSucceed(result);
                memberBean.setPermissions(memberPermissions);
                Intent intent = new Intent();
                intent.putExtra("data", memberBean);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.save) {
            List<MemberBean.MemberPermission> memberPermissions = new ArrayList<>();
            for(String key : mSelectArea.keySet()){
                RoomBean roomBean = mSelectArea.get(key);
                MemberBean.MemberPermission permission = new MemberBean.MemberPermission();
                permission.setTypeId(0);
                permission.setTargetContent(roomBean.getRoomSerialNo());
                memberPermissions.add(permission);
            }
            if(commonAreaIsSelect){
                MemberBean.MemberPermission permission = new MemberBean.MemberPermission();
                permission.setTypeId(0);
                permission.setTargetContent(RoomBean.ROOM_COMMON_SERIALNO);
                memberPermissions.add(permission);
            }
            int type = getInt("type");
            if(type == 1){
                createMember(memberPermissions);
            }else{
                changePermission(memberPermissions);
            }
        } else if (id == R.id.common_area) {
            commonAreaIsSelect = !commonArea.isSelected();
            commonArea.setSelected(!commonArea.isSelected());
            mAdapter.setCanBeEmpty(commonArea.isSelected());
        } else if (id == R.id.reset){
            resetData();
        }
    }
}